package com.zj.osm.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.ToastUtils;
import com.flyco.dialog.widget.NormalDialog;
import com.zj.osm.App;
import com.zj.osm.R;
import com.zj.osm.entity.db.EquipmentDao;
import com.zj.osm.entity.db.LineDao;
import com.zj.osm.entity.db.PhotosDao;
import com.zj.osm.entity.db.PointDao;
import com.zj.osm.entity.greendao.Equipment;
import com.zj.osm.entity.greendao.Line;
import com.zj.osm.entity.greendao.Photos;
import com.zj.osm.entity.greendao.Point;
import com.zj.osm.entity.greendao.Project;
import com.zj.osm.utils.ExportUnit;
import com.zj.osm.view.home.home.home_fragment.onefragment.ProjectAdapterInterface;
import com.zj.osm.view.map.map.MapTdialogInterface;
import com.zj.osm.view.project.projectdetail.ProjectDetailActivity;

import java.util.List;
import java.util.function.Consumer;

import static androidx.constraintlayout.widget.Constraints.TAG;
import static com.blankj.utilcode.util.ActivityUtils.startActivity;
import static com.blankj.utilcode.util.FileUtils.deleteDir;
import static com.zj.osm.utils.Configuration.PROJECT_ID;
import static com.zj.osm.view.home.home.home_fragment.onefragment.OneFragment.DIALOG_BOTTOM;

/**
 * <pre>
 *     author : luhenchang
 *     e-mail : 1276998208@qq.com
 *     time   : 2019/12/03
 *     desc   : 版权所有
 *     version: 1.0
 * </pre>
 */
public class TDialogAdapter extends BaseAdapter {
    @SuppressLint("SdCardPath")
    private Context context;
    private List<Line> list;
    private boolean isCheck = true;
    private MapTdialogInterface mapTdialogInterface;
    public String tLineId = "";

    public String getTLineId() {
        if ((tLineId == null||tLineId.isEmpty() )&& list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getFenZhiLineChecked()) {
                    return list.get(i).getCid();
                }
            }
        }
        return tLineId;
    }

    public TDialogAdapter(Context context, List<Line> list) {
        this.context = context;
        this.list = list;
        //mapTdialogInterface = (MapTdialogInterface) context;
    }

    public TDialogAdapter(Context context, List<Line> list, View interfaceObj) {
        this.context = context;
        this.list = list;
        //mapTdialogInterface = (MapTdialogInterface) interfaceObj;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    @SuppressLint({"InflateParams", "SetTextI18n"})
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_map_t_dialog, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else
            viewHolder = (ViewHolder) convertView.getTag();
        viewHolder.adapter_tv.setText(list.get(position).getName());
        if (list.get(position).getFenZhiLineChecked()) {
            viewHolder.adapter_cb.setChecked(true);
        } else {
            viewHolder.adapter_cb.setChecked(false);
        }
        viewHolder.adapter_cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.forEach(line -> {
                    line.setFenZhiLineChecked(false);
                });
                list.get(position).setFenZhiLineChecked(true);
                Log.e("lineId=", list.get(position).toString());
                tLineId = list.get(position).getCid();
                notifyDataSetChanged();
            }
        });
        return convertView;
    }


    class ViewHolder {
        private TextView adapter_tv;
        private CheckBox adapter_cb;

        ViewHolder(View rootView) {
            this.adapter_tv = rootView.findViewById(R.id.adapter_tv);
            this.adapter_cb = rootView.findViewById(R.id.adapter_cb);
        }

    }

}
